#include "LogApi.hpp"
#include "LogApiImpl.hpp"

#include <stdarg.h>


#ifdef __cplusplus
extern "C"
{
#endif
	namespace cybertron {
		int log_set_rate_limit(unsigned long long rate_per_sec, unsigned long long burst_size)
		{
			return LogApiImpl::_instance->set_rate_limit(rate_per_sec, burst_size);
		}

		int log_enable_net_appender(const char *ip, unsigned short port)
		{
			if (ip == NULL) {
				return -1;
			}
			return LogApiImpl::_instance->enable_net_appender(ip, port);
		}

		int log_enable_console_appender()
		{
			return LogApiImpl::_instance->enable_console_appender();
		}

		int log_enable_file_appender(const char * file_path)
		{
			if (file_path == NULL) {
				return -1;
			}
			return LogApiImpl::_instance->enable_file_appender(file_path);
		}

		void log_log(LOG_LEVEL level, const char * category, const char *producer, const char *fmt, va_list ap)
		{
			if (fmt == NULL) {
				return;
			}
			char buf[1024];
			int pos = vsnprintf(buf, sizeof(buf), fmt, ap);
			LogApiImpl::_instance->log(level, category, producer, buf);
		}

		void log_debug(const char * category, const char *producer, const char *fmt, ...)
		{
			if (fmt == NULL) {
				return;
			}
			va_list ap;
			va_start(ap, fmt);
			log_log(LOG_LEVEL_DEBUG, category, producer, fmt, ap);
			va_end(ap);
		}

		void log_info(const char * category, const char *producer, const char *fmt, ...)
		{
			if (fmt == NULL) {
				return;
			}
			va_list ap;
			va_start(ap, fmt);
			log_log(LOG_LEVEL_INFO, category, producer, fmt, ap);
			va_end(ap);
		}

		void log_warn(const char * category, const char *producer, const char *fmt, ...)
		{
			if (fmt == NULL) {
				return;
			}
			va_list ap;
			va_start(ap, fmt);
			log_log(LOG_LEVEL_WARN, category, producer, fmt, ap);
			va_end(ap);
		}

		void log_error(const char * category, const char *producer, const char *fmt, ...)
		{
			if (fmt == NULL) {
				return;
			}
			va_list ap;
			va_start(ap, fmt);
			log_log(LOG_LEVEL_ERROR, category, producer, fmt, ap);
			va_end(ap);
		}

		void log_fatal(const char * category, const char *producer, const char *fmt, ...)
		{
			if (fmt == NULL) {
				return;
			}
			va_list ap;
			va_start(ap, fmt);
			log_log(LOG_LEVEL_FATAL, category, producer, fmt, ap);
			va_end(ap);
		}
	}
#ifdef __cplusplus
}
#endif